from dotenv import load_dotenv
# 自动加载 .env 文件
load_dotenv()
from graph_init.graph import graph
from utils.logger_config import LoggerSingleton
# 设置日志参数
logger = LoggerSingleton.get_logger()

def main(graph,logger,flag_visual=0):
     # 绘图——可视化
    if flag_visual != 0:
        logger.info('graph可视化')
        from utils.visual_drawing import save_graph_image
        save_graph_image(graph)
    else:
        logger.info('graph未可视化')

    config = {"configurable": {"thread_id": "1"}}
    def stream_graph_updates(user_input: str):
        events = graph.stream(
            {"messages": [{"role": "user", "content": user_input}]},
            config,
            stream_mode="values",
        )
        for event in events:
            event["messages"][-1].pretty_print()

    while True:
        try:
            user_input = input("User(quit\exit\q 退出): ")
            if user_input.lower() in ["quit", "exit", "q"]:
                print("Goodbye!")
                break

            stream_graph_updates(user_input)
        except:
            # fallback if input() is not available
            user_input = "What do you know about LangGraph?"
            print("User: " + user_input)
            stream_graph_updates(user_input)
            break

if __name__ == "__main__":
    main(graph,logger)